package com.bibrApp.difu.actvity.pay_setting_mvp;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.PaySettingResPonseBody;
import com.bibrApp.difu.adapter.AllPaySettingAdapter;
import com.bibrApp.difu.adapter.TradeNameSpinnerAdapter;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.dialog.CustomDialog;
import com.bibrApp.difu.utils.CustomDialogUtils;
import com.bibrApp.difu.utils.SystemClipboardUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.bibrApp.difu.widget.DFDialog;
import com.bibrApp.difu.widget.HorizontalDividerItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class PaySettingActivity extends BaseActivity<PaySettingPresenter> implements IPaySettingView {

    private ImmersionBar mImmersionBar;
    private Context mContext;
    @BindView(R.id.pay_setting_spinner)
    Spinner pay_setting_spinner;
    @BindView(R.id.pay_setting_address_et)
    EditText pay_setting_address_et;

    @BindView(R.id.pay_setting_remark_et)
    EditText pay_setting_remark_et;
    @BindView(R.id.pay_setting_add_btn)
    Button pay_setting_add_btn;

    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    AllPaySettingAdapter allPaySettingAdapter;
    private List<PaySettingResPonseBody> mData = new ArrayList<>();

    @Override
    protected void initPresenter() {
        presenter = new PaySettingPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pay_setting;
    }

    @Override
    public void initView() {
        mContext = this;
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init();

        initSpinner();

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .color(getResources().getColor(R.color.black_f0))
                .margin(20)
                .build());
        allPaySettingAdapter = new AllPaySettingAdapter();
        recyclerView.setAdapter(allPaySettingAdapter);

        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            presenter.getAllPaySetting();
        });
        allPaySettingAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(mData == null || mData.size() == 0){
                    return;
                }
                if(view.getId() == R.id.item_copy_pay_setting){
                    if (!TextUtils.isEmpty(mData.get(position).getWalletAddress())) {
                        ToastUtils.ShowToast(mContext, "地址复制成功");
                        SystemClipboardUtils.copyToClipboard(mData.get(position).getWalletAddress(),mContext);
                    } else {
                        ToastUtils.ShowToast(mContext, "地址为空");
                    }
                }
            }
        });
    }

    private void initSpinner() {
        String[] tradeName = getResources().getStringArray(R.array.paySettingTradeName);
        TradeNameSpinnerAdapter arrayAdapter = new TradeNameSpinnerAdapter(mContext);
        arrayAdapter.setDatas(Arrays.asList(tradeName));
        pay_setting_spinner.setAdapter(arrayAdapter);
        pay_setting_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                pay_setting_spinner.setSelection(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                pay_setting_spinner.setSelection(0);
            }
        });
    }


    @Override
    public void error(String msg) {
        ToastUtils.ShowToast(mContext, msg);
    }

    @Override
    public void success(String msg) {

    }

    @Override
    public void successOfAllPaySetting(List<PaySettingResPonseBody> paySettingResPonseBodyList) {
        if(paySettingResPonseBodyList != null && paySettingResPonseBodyList.size() >0){
            smartRefreshLayout.finishRefresh();
            mData.clear();
            mData.addAll(paySettingResPonseBodyList);
            allPaySettingAdapter.setNewData(mData);
        }else{
            smartRefreshLayout.finishRefresh();
            mData.clear();
            allPaySettingAdapter.notifyDataSetChanged();
        }
    }

    @OnClick(R.id.return_iv)
    public void clickBack(){
        finish();
    }
    @OnClick(R.id.pay_setting_add_btn)
    public void addPaySetting(){
        String addressStr = pay_setting_address_et.getText().toString();
        String remarkStr = pay_setting_remark_et.getText().toString();
        if(TextUtils.isEmpty(addressStr)){
            ToastUtils.ShowToast(mContext,"地址不能为空");
            return;
        }
        if (TextUtils.isEmpty(remarkStr)) {
            ToastUtils.ShowToast(mContext,"备注不能为空");
            return;
        }
        showConfirmDialog(addressStr , remarkStr);

    }

    private void showConfirmDialog(String addressStr ,String remarkStr) {
        new DFDialog.Builder(this)
                .setTitleVisible(View.GONE)
                .setMessage("注意：USDT,ETH,BTC添加收付款后，作为该币种唯一认定收付款地址。在转账时候，为了保护您的权益，请您不要用其他地址转账，如需修改，请联系客服。\n\n" +
                        "借款人：绑定后的USDT，作为您的收款地址和还款时候的还出地址。\n\n" +
                        "出借人：绑定后的USDT，作为您的付款地址和还款时候的还入地址。")
                .setSure(true)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showSecurityCodeDialog(addressStr,remarkStr);
                    }
                })
                .show();
    }

    /**
     * 资金安全码弹窗
     */
    private void showSecurityCodeDialog(String address , String remark) {
        CustomDialogUtils.showEditDialog(mContext, "请输入资金安全吗", "确认", new CustomDialog.OnEditClickListener() {
            @Override
            public void onPositiveClick(View view, String text) {
                DfApp dfApp = (DfApp) getApplication();
                if(!TextUtils.isEmpty(text) && text.length() >= 6){
                    presenter.addPatSetting(remark,dfApp.getUserId(),dfApp.getuId(),address,text, (String) pay_setting_spinner.getSelectedItem());
                }else{
                    ToastUtils.ShowToast(mContext,"资金安全码错误");
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }

    }

}
